package com.wyp168.leetcode.leetcode.hot100;

/**
 * @ClassName Code6
 * @Title TODO 字符串Z型转换
 * @Description TODO
 * @Author wyp168
 * @Date 2022/10/9 22:43
 */
public class Code6 {
    public static void main(String[] args) {
        Code6 solution = new Code6();
        String s = "PAYPALISHIRING";
        int numRows = 4;
        System.out.println(solution.convert(s, numRows));
    }

    public String convert (String s, int numRows) {
        if (numRows < 2 || s.length() <= numRows) {
            return s;
        }
        boolean isSort = true;
        int r = 0;
        StringBuilder[] rowStrings = new StringBuilder[numRows];
        for (int i = 0; i < numRows; i++) {
            rowStrings[i] = new StringBuilder();
        }
        int i = 0;
        while (isSort && i < s.length()) {
            rowStrings[r++].append(s.charAt(i++));
            if (r == numRows - 1) {
                isSort = false;
//                r --;
            }
            while (!isSort && i < s.length()) {
                rowStrings[r--].append(s.charAt(i++));
                if (r == 0)
                    isSort = true;
            }
        }
        String result = "";
        for (int j = 0; j < numRows; j++) {
            result += rowStrings[j].toString();
        }
        return result;
    }

    //PAHNAPLSIIGYIR
    //PALIRAPIHIGYSN


    //PINALSIGYAHRPI
}
